package cn.imnaer.common.factory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

    public static void saveTextToFile(String path, String text) {
        FileOutputStream outImgStream = null;
        try {

            byte[] bytes = text.getBytes("UTF-8");
            File file = new File(path);
            outImgStream = new FileOutputStream(file);
            outImgStream.write(bytes);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outImgStream != null) {
                try {
                    outImgStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    public static String readFile(String path) {
        FileInputStream fis = null;
        File file = new File(path);
        String result = "";
        try {
            fis = new FileInputStream(file);
            byte[] cache = new byte[1024];
            List<Byte> temp = new ArrayList<Byte>();
            int readCount;
            while ((readCount = fis.read(cache)) != -1) {
                for (int i = 0; i < readCount; i++) {
                    temp.add(cache[i]);
                }
            }

            byte[] bytes = new byte[temp.size()];
            for (int i = 0; i < temp.size(); i++) {
                bytes[i] = temp.get(i);
            }
            result = new String(bytes, "UTF-8");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static byte[] readImage(String path) {
        FileInputStream fis = null;
        File file = new File(path);
        byte[] result = new byte[0];
        try {
            fis = new FileInputStream(file);
            byte[] cache = new byte[1024];
            List<Byte> temp = new ArrayList<Byte>();
            int readCount;
            while ((readCount = fis.read(cache)) != -1) {
                for (int i = 0; i < readCount; i++) {
                    temp.add(cache[i]);
                }
            }

            byte[] bytes = new byte[temp.size()];
            for (int i = 0; i < temp.size(); i++) {
                bytes[i] = temp.get(i);
            }
            result = bytes;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static boolean isExists(String path) {
        File file = new File(path);
        return file.exists();
    }
}